
import 'package:amap_all_fluttify/amap_all_fluttify.dart';
import 'package:flui/flui.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:fzcgx_app/ui/widget/custom_appbar_widget.dart';
import 'package:permission_handler/permission_handler.dart';

class SelectLocationPage extends StatefulWidget {
  final Function callback;
  SelectLocationPage({Key key,this.callback})
      : super(key: key);
  @override
  _SelectLocationPageState createState() => _SelectLocationPageState();
}

class _SelectLocationPageState extends State<SelectLocationPage> {
  // ignore: unused_field
  bool _requestPermissionIsGranted = false;
  // ignore: unused_field
  PoiInfo _poiInfo;

  @override
  void initState() {
    _checkPermission();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppbar(
        title: '选择地址',
      ),
      body: Stack(
        children: <Widget>[
          LocationPicker(
       zoomGesturesEnabled: true,
        requestPermission: () {
          return Permission.location.request().then((it) => it.isGranted);
        },
        poiItemBuilder: (poi, selected) {
          return ListTile(
            title: Text(poi.title),
            subtitle: Text(poi.address),
            trailing: selected ? Icon(Icons.check) : SizedBox.shrink(),
          );
        },
        onItemSelected: (point){
          _poiInfo=point;
          print('kk:$point');
        },
      ),
      Align(
        alignment: Alignment.bottomCenter,

        child: InkWell(
          onTap: (){
            if(null == _poiInfo){
               FLToast.showText(text: '请选择地址');
               return;
            }
            if(null != widget.callback){
             Navigator.pop(context);
              widget.callback(_poiInfo);

            }
          },
          child: Container(
          color: Color(0xff15A1FF),
          width: ScreenUtil.getInstance().screenWidth,
          height: ScreenUtil.getInstance().getHeight(44),
          alignment: Alignment.center,
          child: Text('确定选择', style: TextStyle(
                color: Colors.white,
                fontSize: ScreenUtil.getInstance().getSp(14),
                fontWeight: FontWeight.w400)),
        ),
        )
      )
        ],
      )
    );
  }

 
  _requestPermission() async {
    var platform = Theme.of(context).platform;
    if (TargetPlatform.android == platform) {
      _requestAndroidPermission();
    } else if (TargetPlatform.iOS == platform) {
      _requestIosPermission();
    }
  }

  
  _requestAndroidPermission() async {
    if (await Permission.locationAlways.request().isGranted) {
      if (await Permission.storage.request().isGranted) {
        if (await Permission.phone.request().isGranted) {
          // Either the permission was already granted before or the user just granted it.
          if (mounted) {
            setState(() {
              print('gred');
              _requestPermissionIsGranted = true;
            });
          }
        }
      }
    }
  }

 
  _requestIosPermission() async {
    if (await Permission.locationAlways.request().isGranted) {
      if (await Permission.storage.request().isGranted) {
        // Either the permission was already granted before or the user just granted it.
        if (mounted) {
          setState(() {
            _requestPermissionIsGranted = true;
          });
        }
      }
    }
  }

  _checkPermission() async {
    bool status = await Permission.locationAlways.isGranted;
    //判断如果还没拥有权限就申请获取权限
    if (status) {
      //有定位权限
      if (mounted) {
        setState(() {
          _requestPermissionIsGranted = true;
        });
      }
    } else {
      //没有定位权限
      _requestPermission();
    }
  }
}
